/** ***************************************************************************
 * @file    RDP_main.cpp
 * @mainpage
 * @date    2013-12-22 13:48
 * @author  Alexey Kulikov <alexeyqu@gmail.com>, group 376, DCAM MIPT
 * @version 1.00
 *
 * Parseur basé sur descente recursive.
 *
 * @todo
 *      - Realiser G0 ... G5 grammaires en s'accordant à la cours
******************************************************************************/

#include "RDPTB.h"

token_t* s = NULL;
FILE* The_Log;
FILE* outstream;

int main()
{
    time_t t_now = time (0);

    struct tm* now = localtime (&t_now);

    printf ("Bonjour!\n");
    printf ("Je suis une programme machine\n"
            "qui peut construire une arbre binaire d'une expression mathematique.\n"
            "J'utilise la méthode de descente recursive.\n\n");
    printf ("Auteur: AQ\n");
    printf ("Est compilé de %s fichier\n"
            "à %s %s\n", __FILE__, __DATE__, __TIME__);
    printf ("Est executé à %d-%d-%d %d:%d:%d\n", now->tm_mon + 1, now->tm_mday, now->tm_year + 1900,
            now->tm_hour, now->tm_min, now->tm_sec);
    printf ("--------------------\n\n");

    token_t* str = (token_t*) calloc (Max_Len, sizeof (token_t));
    FILE* instream =  fopen ("input.txt",  "r");
    outstream = fopen ("output.dot", "w");
    The_Log = fopen ("log.txt", "w");

    ASSERT (instream); ASSERT (outstream); ASSERT (The_Log);

    Get_String (instream, str);

    root_t* my_tree = NULL;

    Tree_Construct (&my_tree, 0);

    Get_G4 (str, my_tree);

    Tree_Dump (The_Log, my_tree);

    Tree_Find (my_tree, my_tree->root, DIFF_TYPE);

    Tree_Optimizer (my_tree);
    Tree_Dump (The_Log, my_tree);

    Tree_Print_Dot (outstream, my_tree);

    Tree_Destruct (my_tree);

    fclose (outstream);
    fclose (instream);
    fclose (The_Log);

    return 0;
}
